/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TabButton.java
 *
 * Created on 2009-2-18, 8:59:12
 */

package com.web.ui.common.component;

import com.web.ui.UIApp;
import com.web.ui.UIMain;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author Administrator
 */
public class TabButton extends javax.swing.JPanel {
    
    private ResourceMap  resourceMap = Application.getInstance(UIApp.class).getContext().getResourceMap(UIMain.class);
    private Image image = ((ImageIcon)resourceMap.getIcon("bt_bg2.icon")).getImage();

    private boolean active = false;

    @Override
    public JPopupMenu getComponentPopupMenu() {
        return super.getComponentPopupMenu();
    }

    private String title = "";

    public void setActive(boolean active){
        this.active = active;
    }

    public boolean getActive(){
        return this.active;
    }

    /** Creates new form TabButton */
    public TabButton(Image image) {
        
        this.image = image;
        initComponents();
    }

    public TabButton() {
        initComponents();
    }

    /** Creates new form TabButton */
    public TabButton(String title) {
        initComponents();
        setTitle(title);
    }

    public void setTitle(String title){
        this.title = title;
    }

    public Image getImage(){
        return this.image;
    }

    public void setImage(Image image){
        this.image = image;
    }

    public String getTitle(){
        return this.title;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabTitle = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(49, 20));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(150, 18));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        setLayout(new java.awt.BorderLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(TabButton.class);
        tabTitle.setBackground(resourceMap.getColor("tabTitle.background")); // NOI18N
        tabTitle.setIcon(null);
        tabTitle.setText(resourceMap.getString("tabTitle.text")); // NOI18N
        tabTitle.setName("tabTitle"); // NOI18N
        tabTitle.setPreferredSize(new java.awt.Dimension(90, 18));
        add(tabTitle, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized

        //getGraphics().drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
       // this.updateUI();
    }//GEN-LAST:event_formComponentResized

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);

        if(active){
            this.setImage(image);
            graphics.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
            Image image2 = ((ImageIcon)resourceMap.getIcon("bt_bg2_close.icon")).getImage();
            graphics.drawImage(image2, 120, 5, image2.getWidth(this), image2.getHeight(this),this);
            graphics.setColor(Color.white);
            graphics.drawString(getTitle(), 5, 15);
        } else if(!active) {
            Image image3 = ((ImageIcon)resourceMap.getIcon("bt_bg1.icon")).getImage();
            graphics.drawImage(image3, 0, 0, this.getWidth(), this.getHeight(), this);
            Image image2 = ((ImageIcon)resourceMap.getIcon("bt_bg1_close.icon")).getImage();
            graphics.drawImage(image2, 120, 5, image2.getWidth(this), image2.getHeight(this),this);
            graphics.setColor(Color.black);
            graphics.drawString(getTitle(), 5, 15);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel tabTitle;
    // End of variables declaration//GEN-END:variables

}
